class StatisticVehicleModel extends Backbone.Model
    url: '/statistic/vehicle'
    initialize: (options) =>
        @fetch()

class VehicleDetailModel extends Backbone.Model
    url: '/mytable/getModel?s=table.creators.VehicleTable'
    initialize: (options) =>
        @fetch()

class StatisticVehicleView extends Marionette.ItemView
    template: "#vehicle_status_template"
    initialize: =>
        @listenTo @model, "change", @on_model_change
        @listenTo @, "all", @onThisAll
    onThisAll: (name) => log "StatisticVehicleView on this event: " + name
    on_model_change: =>
        log "StatisticVehicleView on its model changed."
        
class StatisticNoticeView extends Marionette.ItemView
    template: "#notice_template"
    initialize: =>
        @listenTo @model, "change", @on_model_change
        @listenTo @, "all", @onThisAll
    onThisAll: (name) => log "StatisticNoticeView on this event: " + name
    on_model_change: =>
        log "StatisticNoticeView on its model changed."

class StatisticVehicleLayout extends Marionette.Layout
    template: "#statistic_vehicle_layout"
    events: =>
        'click #onRepair': (e) =>
            model = new VehicleDetailModel();
            @listenTo model, "change", @show_vehicle_detail
    show_vehicle_detail: (model) =>
        if (model.has('aoColums'))
            tableView = new ModelTableView(model: model)
            @region_detail.show tableView
    initialize: => 
        if (@model)
            @listenTo @model, "change", @on_model_change
        @listenTo @, "all", @onThisAll
        @listenTo @, "dom:refresh", @onDomRefresh
    onThisAll: (name) => log "StatisticVehicleLayout on this event: " + name
    on_model_change: =>
        log "StatisticVehicleLayout on its model changed. ReRender..."
        @render()
    onRender: =>  
        if (@model.has('vehicle_status'))
            @region_vehicle.show new StatisticVehicleView(model: @model)
        if (@model.has('safecheck_notices'))
            @region_notice.show new StatisticNoticeView(model: @model)
    regions:
        region_vehicle: "#vehicle_status"
        region_notice: "#safecheck_notice"
        region_detail: "#vehicle_detail"
   
#--------------------------------------------------controler
class StatisticVehicleApp
    search: =>
        myapp.mainRegion.close()
        layout = new StatisticVehicleLayout(model: new StatisticVehicleModel())
        myapp.mainRegion.show(layout)
        


#--------------------------------------------------router
class StatisticVehicleAppRouter extends Backbone.Marionette.AppRouter
    appRoutes: 
        "statistic/vehicle": "search"

                
myapp.addInitializer -> 
    myapp.StatisticVehicleApp = new StatisticVehicleApp()
    myapp.StatisticVehicleApp.router = new StatisticVehicleAppRouter controller: myapp.StatisticVehicleApp

myapp.vent.trigger("routing:started")
